Welcome back. In all our turtle examples,
I glossed over the import statement.
In this specialization, we're trying to minimize the amount of magic.
We want you to be able to read code and reason about it.
So this video provides some details about that import statement.
There are lots of functions and methods available to you in Python,
that other people have already written and we can just invoke them.
We don't have to write them ourselves.
Some of those functions are built into the core of Python,
like the len function.
You can just invoke it, it's always there.
Lots of other functions are not built into the core of Python that are readily available.
They're part of a standard library of modules.
A module is a code file that defines some functions that can be used by other programs.
Before you can invoke a function that's from a standard library module,
you need to tell the Python interpreter to you make it
available and that's what the import statement does.
Turtle is one of those standard library modules,
but we'll come back to that in a minute.
Let's look at a simpler module first.
There's a module called, "random" and you just
have to import it in order to make all of its functions available.
One of its functions is called, "randrange",
you can see this on line six.
So we've imported the random module and then on line six,
we asked for randrange which says,
"Give me an integer between one up to,
but not including seven."
So one, two, three, four, five, or six.
There's another function called
random and I'll talk in a minute about this weird thing of random.random.
We have this random function that returns a floating point number between zero and one.
In this case, we got 0.72 and then we get a die roll, a one,
two, three, four, five, or six from the randrange function.
If I do it again, I may get different values, get 0.039 and three.
Run it a third time, I'll get yet again different values.
There are two possible syntaxes for importing and referring the stuff from modules.
The first makes the stuff available that doesn't give it any special alias.
That's what this version that I'm showing in the current code does.
On line one, we make everything from the random module available,
but every time we want to refer to it,
we have to say random dot.
It doesn't give us any alias.
So I say random dot and then the name of the function that's
defined within the random module like randrange or random.
Notice that this is another use of the dot notation.
Before we've seen something like tess.forward,
where tess was a turtle object and dot forward and we would say,
how many pixels to go forward.
Forward is a method that can be applied to tess, the turtle object.
Here we're saying random.randrange.
It's a little bit analogous.
We're getting something from the random module
and that something is the randrange function.
But it's not exactly the same, random is a module,
not the kind of Python object we've been seeing so far.
The way to tell the difference between these two different uses
of the dot notation is just by the context.
If we said, import random as we did on line one then random dot something,
we'll find this something object inside that module.
Anyway, that explains the funny random.random on line three.
Random is the name of the module,
that gets us the first random and inside that module,
there's a definition of a function also called random.
That gives us the second random.
Similarly, random.randrange, random is the name of the module,
randrange is a function inside that module.
One thing we can't say is randrange.random,
that is going to fail.
We do that. The second approach
to importing stuff from other modules
puts the functions directly into our local namespace.
So we don't have to keep referring to the module they came from.
Let me show you how that works.
Instead of saying import random.
We're going to say from random import randrange.
We can also import the random function while we're at it.
Now, when we refer to them,
we no longer have to say what module they came from.
Those names become available in
our local namespace and this will work just like the other one.
We got a new value there when we ran it again.
Either these two different ways works,
either the comment to that version that I have on line one import random
or the other version on line two from random import randrange and random.
But you have to refer to things a little differently depending on which one you did.
So if we say from random,
import randrange, then we'll just refer to random.
If we did the other one,
we will have to say random.random on line four.
With that understanding of module importing,
let's take a look back at how we imported the turtle module.
Let's look at lines one through three here.
On line one, we say import turtle,
make everything from the turtle module available to us.
Then on lines two and three,
we're doing that turtle dot thing.
On line two, we do turtle.Screen and we haven't
explicitly talked about classes yet and you
won't talk about that until much later in the specialization.
But this is creating a new instance of a class,
the class capital Screen is defined in the turtle module.
Similarly, the class turtle is defined.
So it's like we're invoking a function.
But it's a function that creates a new turtle object.
On line two, it is creating a new screen object.
So this is just an example of the same syntax we were seeing with import random.
Previously, we're importing the turtle module and
then we're referring to something screen,
that is defined inside of the turtle module.
By the way we only have a few of
the standard library modules that would normally be available in Python.
Only a few of those are available in this Runestone textbook environment.
We have the random module,
the turtle module, and the math module,
but there are a lot more that are part of
the standard distribution if you just install Python on your own machine,
and it can do lots of stuff for you so you don't have to write very much code yourself.
You just have to find the right module.
Then even beyond the standard library,
there's lots of other modules that you can install
if you've installed Python on your local machine.
By the way, if you haven't seen XKCD before,
there's lots of great geeky humor there and I highly recommend it.
Here's one of my favorites.
A friend is talking to his other friend who's up in the sky says,
''You're flying, how?'' his friend says,
''Python'', which I hope you're also experiencing.
You're using Python and you're flying high.
He says, ''I learned it last night. Everything is so simple.
Hello World is just print hello world.
Of course, in the newer version of Python that we have these days,
we would have to have parentheses but still very simple.''
His friend says, ''I don't know about Python.
I've heard it's got these weird things like dynamic typing and whitespace matters.''
His friend who's up in the sky says,
''Come join us. Programming is fun again. It's a whole new world up here."
"But how are you flying?"
"Well, I just typed import anti-gravity. That's it.
Well, I also sampled everything in the medicine cabinet for comparison
but I'm pretty sure it's the Python."
So, Python has lots of great modules. Just import them.
I don't know about the anti-gravity module though.
Well, see you next time.